package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
/**
 * @Author: lh
 * @Date: 2023/5/2 16:06
 * @Package:cn.cvs.controller
 */

/**
 *      Spring MVC 控制器
 */
@Controller
@RequestMapping("/hello")  // 表示访问本类中的方法，都需要添加这个前缀，不然404
public class HelloController {

    /**
     *  GET:查询  POST:保存  PUT：修改    DELETE:删除
     * @return
     */
    @RequestMapping(value = "hello",method = RequestMethod.GET) // get方式
    // 获取客户端数据，只需形参和客户端变量名相同即可
    //@RequestParam()
    // required ：设置属性是否必填
    public String hello(@RequestParam(value = "userName",required = false) String name){
        System.out.println("Spring MVC 框架搭建成功 。。。");

        System.out.println("姓名：" + name);
        return "/hello.jsp";
    }

    /**
     *  示例14：ModelAndView
     */
    @RequestMapping("/ModelAndView")
    public ModelAndView hello3(@RequestParam String realName){
        System.out.println("你好：" + realName);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("realName",realName);
        modelAndView.setViewName("hello");

        return modelAndView;
    }


    /**
     *  示例 16：Model
     * @return
     */
    @RequestMapping("/hello2")
    public String hello2(Model model,@RequestParam String realName){
        System.out.println("你好：" + realName);
        model.addAttribute("realName",realName);
        return "/hello.jsp";
    }

    /**
     *  示例 17：Model
     * @return
     */
    @RequestMapping("/hello3")
    public String hello3(Model model,@RequestParam String realName){
        System.out.println("你好：" + realName);
        model.addAttribute("realName",realName);
        model.addAttribute(realName);

        return "/hello.jsp";
    }

    /**
     *  示例 18：Model
     * @return
     */
    @RequestMapping("/hello4")
    public String hello4(Model model,@RequestParam String realName){
        System.out.println("你好：" + realName);
        model.addAttribute("realName",realName);
        model.addAttribute(realName);

        SysUser sysUser = new SysUser();
        sysUser.setRealName(realName);
        model.addAttribute("currentUser",sysUser);
        model.addAttribute(sysUser);

        return "/hello.jsp";
    }

    /**
     *  示例 19：Map
     * @return
     */
    @RequestMapping("/hello5")
    public String hello4(Map<String,Object> map,@RequestParam String realName){
        System.out.println("你好：" + realName);

        map.put("realName",realName);

        return "/hello.jsp";
    }


}
